import React, { Component } from "react";
// import { connect } from 'react-redux';
import store from "../redux/store";

export default class index extends Component {
  state = { count: 1 };

  //手动更新到render方法，不然redux里面的state值无法手动更新，也在直接写到app.js上面去，只要redux里面的值有更新，就直接render整个页面
//   componentDidMount() {
//     store.subscribe(() => {
//       this.setState({});
//     });
//   }

  increment = () => {
    const { value } = this.selectNumber;
    console.log({value});
    
    store.dispatch({ type: "increment", data: value * 1 });
  };
  delIncrement = () => {
    const { value } = this.selectNumber;
    store.dispatch({ type: "delIncrement", data: value * 1 });
  };
  incrementIfOdd = () => {
    const { value } = this.selectNumber;
    const count = store.getState();
    if (count % 2 !== 0) {
      store.dispatch({ type: "increment", data: value * 1 });
    }
  };
  incrementAsync = () => {
    setTimeout(() => {
      const { value } = this.selectNumber;
      store.dispatch({ type: "increment", data: value * 1 });
    }, 1000);
  };

  render() {
    return (
      <div>
        <h1>当前求和为：{store.getState()}</h1>
        <select ref={(c) => (this.selectNumber = c)}>
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>
          <option value="4">4</option>
        </select>
        &nbsp;
        <button onClick={this.increment}>+</button> &nbsp;
        <button onClick={this.delIncrement}>-</button> &nbsp;
        <button onClick={this.incrementIfOdd}>当前求和为奇数再加</button> &nbsp;
        <button onClick={this.incrementAsync}>异步求和</button>
      </div>
    );
  }
}
